{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pymongo import MongoClient\n",
    "from bson.objectid import ObjectId"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "client = MongoClient('act4dgem.cse.iitd.ac.in', serverSelectionTimeoutMS=600000)\n",
    "db = client['eventwise_media-db']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "resolved_entities_1 = db.get_collection('all_media_entities_resolved_final')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "resolved_entities_2 = db.get_collection('all_media_entities_resolved_final3')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('./relationships_works_in.txt') as fil:\n",
    "    lines = fil.readlines()\n",
    "instances = [l[10:].strip().split('\\t')[:2] for l in lines if l.startswith('instance')]\n",
    "sentences = [l[10:].strip() for l in lines if l.startswith('sentence')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "name = []\n",
    "name_uuid = []\n",
    "org = []\n",
    "org_uuid = []\n",
    "sen = []\n",
    "for i,inst in enumerate(instances):\n",
    "    res = resolved_entities_1.find_one({'_id':ObjectId(inst[0]), 'graphid':{'$exists':True}, 'type':'Person'})\n",
    "    s = sentences[i]\n",
    "    if res:\n",
    "        name.append(res['stdName'])\n",
    "        name_uuid.append(res['graphid'])\n",
    "        s = s.replace(inst[0], res['stdName'])\n",
    "    else:\n",
    "        continue\n",
    "    res = resolved_entities_2.find_one({'_id':ObjectId(inst[1]), 'graphid':{'$exists':True}})\n",
    "    if res:\n",
    "        org.append(res['stdName'])\n",
    "        org_uuid.append(res['graphid'])\n",
    "        s = s.replace(inst[1], res['stdName'])\n",
    "    else:\n",
    "        del name[-1]\n",
    "        continue\n",
    "    sen.append(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(name) == len(org)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('relatedInstancesWorksIn.csv', 'w') as fil:\n",
    "    writer = csv.writer(fil)\n",
    "    for i in range(len(name)):\n",
    "        writer.writerow([name[i], name_uuid[i], org[i], org_uuid[i], sen[i]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "294"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "294"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(sen)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "294"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(org)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('relatedInstancesInGraphDb_WorksIn.txt', 'w') as fil:\n",
    "    for i in range(len(sen)):\n",
    "        print(name[i], '\\t' , org[i], '\\n', sen[i], '\\n', '-'*50, file=fil)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
